﻿#region Imports
using System;
using System.Globalization;
using System.Windows.Data;
#endregion

namespace CommonUtilities.Converters
{
    public class UIOffsetConverter : IValueConverter
    {
        #region IValueConverter implementation
        /// <summary>
        /// Adjusts property value of UI element against specified offset
        /// </summary>
        /// <param name="value">Size as <code>System.Double</code></param>
        /// <param name="targetType">UI element property of type <code>System.Double</code></param>
        /// <param name="parameter">Value of offset as <code>System.Double</code></param>
        /// <param name="culture">UI culture of thread</param>
        /// <returns>Adjusted size of the UI element against specified offset</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double offset = GetOffset(parameter);
            return (double)value - offset;
        }

        /// <summary>
        /// Reverts to original property value from adjusted value
        /// </summary>
        /// <param name="value">Adjusted size as <code>System.Double</code></param>
        /// <param name="targetType">UI element property of type <code>System.Double</code></param>
        /// <param name="parameter">Value of offset as <code>System.Double</code></param>
        /// <param name="culture">UI culture of thread</param>
        /// <returns>Original property value of the UI element against specified offset</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double offset = GetOffset(parameter);
            return (double)value + offset;
        }
        #endregion IValueConverter implementation

        #region Methods
        /// <summary>
        /// Calculates offset from converter parameter
        /// </summary>
        /// <param name="parameter">Offset value in <code>System.Double</code> passed as converter parameter</param>
        /// <returns>Offset value in <code>System.Double</code></returns>
        private double GetOffset(object parameter)
        {
            double offset = 30;
            try { offset = (double)parameter; }
            catch (Exception) { }

            return offset;
        }
        #endregion Methods
    }
}
